/**
 *  版权所有 Copyright@2016年1月29日
 */
package com.fairy.core.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
 * [HttpWebUtil.java]
 *
 * @ProjectName: [dubbo_core]
 * @Author: [waysun]
 * @CreateDate: [2016年1月29日 上午11:37:48]
 * @Update: [说明本次修改内容] BY [waysun] [2016年1月29日 上午11:37:48]
 * @Version: [v1.0]
 */

public class HttpUtil {
	public String getHTMLSrc(String url){  
        InputStream openStream = null;  
        BufferedReader buf = null;  
        StringBuffer sb=new StringBuffer();
        try {  
            String line = null;  
            URL theUrl= new URL(url);  
            openStream = theUrl.openStream();  
			//<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
			//构建输入流的的字符集必须和HTML源码中的 charset一致     
			buf = new BufferedReader(new InputStreamReader(openStream,"utf-8"));  
              
            while((line = buf.readLine()) != null){  
                System.out.println(line); 
                sb.append(line+"\r\n");
              
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            try {  
                if(openStream!=null){  
                    openStream.close();  
                }  
                if(buf!=null){  
                    buf.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return sb.toString();
  
    }  
    /**
     * 网页抓取方法
     *
     * @param urlString
     *            要抓取的url地址
     * @param charset
     *            网页编码方式
     * @param timeout
     *            超时时间
     * @return 抓取的网页内容
     * @throws IOException
     *             抓取异常
     */
    public static String GetWebContent(String urlString, final String charset,
            int timeout) throws IOException {
        if (urlString == null || urlString.length() == 0) {
            return "";
        }
        urlString = (urlString.startsWith("http://") || urlString
                .startsWith("https://")) ? urlString : ("http://" + urlString)
                .intern();
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Pragma", "no-cache");
        conn.setRequestProperty("Cache-Control", "no-cache");
 
        int temp = Integer.parseInt(Math.round(Math.random()
                * (UserAgent.length - 1))
                + "");
        conn.setRequestProperty("User-Agent", UserAgent[temp]); // 模拟手机系统
        conn.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");// 只接受text/html类型，当然也可以接受图片,pdf,*/*任意，就是tomcat/conf/web里面定义那些
        conn.setConnectTimeout(timeout);
        try {
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "";
            }
        } catch (Exception e) {
            try {
                System.out.println(e.getMessage());
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            return "";
        }
        InputStream input = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input,
                charset));
        String line = null;
        StringBuffer sb = new StringBuffer("");
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\r\n");
        }
        if (reader != null) {
            reader.close();
        }
        if (conn != null) {
            conn.disconnect();
        }
        return sb.toString();
    }
    /**
	 *  发送get请求
	 *
	 */
	public static String sendGet(String url,String param){
		String result = "";
		BufferedReader in = null;
		try{
			String urlNameString = url + "&" + param;
			URL realUrl = new URL(urlNameString);
			// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立实际的连接
			connection.connect();
			// 获取所有响应头字段
			Map<String,List<String>> map = connection.getHeaderFields();
			// 遍历所有的响应头字段
			for(String key: map.keySet()){
				System.out.println(key + "--->" + map.get(key));
			}
			// 定义 BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while((line = in.readLine()) != null){
				result += line;
			}
		}catch(Exception e){
			System.out.println("发送GET请求出现异常！" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输入流
		finally{
			try{
				if(in != null){
					in.close();
				}
			}catch(Exception e2){
				e2.printStackTrace();
			}
		}
		return result;
	}
    public static String[] UserAgent = {
            "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.2",
            "Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.11",
            "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18121",
            "Nokia5700AP23.01/SymbianOS/9.1 Series60/3.0",
            "UCWEB7.0.2.37/28/998", "NOKIA5700/UCWEB7.0.2.37/28/977",
            "Openwave/UCWEB7.0.2.37/28/978",
            "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/989" 
            };
 
}





